package com.codbking.calendar.exaple;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.codbking.calendar.exaple.greendao.gen.DaoMaster;
import com.codbking.calendar.exaple.greendao.gen.DaoSession;
/**
 * Created by SensYang on 2019/06/12 23:20
 */
public class GreenDaoUtils {
    /**
     * Query query = getStudentDao().queryBuilder()
     * .where(StudentDao.Properties.SName.eq(curCondition))
     * .build();
     * // 查询结果以 List 返回
     * List students = query.list();
     */

    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    private static GreenDaoUtils greenDaoUtils;

    private GreenDaoUtils() {}

    public static GreenDaoUtils getSingleTon() {
        if (greenDaoUtils == null) {
            greenDaoUtils = new GreenDaoUtils();
        }
        return greenDaoUtils;
    }

    private void initGreenDao() {
        if (CEApplication.getInstance() != null) {
            mHelper = new DaoMaster.DevOpenHelper(CEApplication.getInstance(), "notes_db", null);
            db = mHelper.getWritableDatabase();
            mDaoMaster = new DaoMaster(db);
            mDaoSession = mDaoMaster.newSession();
        }else{
            Log.e("log","上下文对象为空");
        }
    }

    public DaoSession getmDaoSession() {
        if (mDaoMaster == null) {
            initGreenDao();
        }
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        if (db == null) {
            initGreenDao();
        }
        return db;
    }

}
